package com.baosight.sczx.zm.ci.service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.zm.ci.domain.ZMCIWorkGroupInfo;


/**
 * 维护班组信息
 * <p>Title: ServiceZMCI60.java</p>
 * <p>Description: </p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 王伟
 * @version 1.0
 * <pre>Histroy:
 *          2016-8-31 09:25:03  王伟  Create
 *</pre>
 */

public class ServiceZMCI60 extends ServiceEPBase{
	/**
	 * 初始化
	 * <p>Function: initLoad</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCI60</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-8-31 09:25:03  王伟
	 * </p>
	 *
	 */
	@Override
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");//如果不加这个，翻页的统计和显示是错误的		
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	/**
	 * 查询
	 * <p>Function: query</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-8-31 10:58:37 王伟
	 * </p>
	 *
	 */
	@Override
	public EiInfo query(EiInfo inInfo) {
		
		StaticTools.setQueryParameters(inInfo, "fdMiningId", UserSession.getInSessionProperty("miningCode"), true);
		EiInfo outInfo = super.query(inInfo, "ZMCIWorkGroupInfo.query", new ZMCIWorkGroupInfo());
		
		String fdMiningId = (String)UserSession.getInSessionProperty("miningCode");
	    String departmentId = (String)UserSession.getInSessionProperty("orgCode"); 
	    
		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.ci.department", "orgCode", "orgName", true,fdMiningId));
//		outInfo.addBlock(WorkSchedule.getWorkScheduleBlock("cmes.cp.workNumInqu", true));
		outInfo.addBlock(StaticBlocks.GetBusinessType());//班組的工作类型
		outInfo.addBlock(StaticBlocks.GetUseingStatus(true));//班組的工作类型
		outInfo.set("userId", UserSession.getUserId());
		outInfo.set("userName", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("departmentId", departmentId);
		outInfo.set("departmentName", UserSession.getInSessionProperty("orgName"));
		outInfo.set("fdCreatorId", UserSession.getUserId());//登录人的默认值
		outInfo.set("fdMiningId", fdMiningId);//矿ID的默认值
		outInfo.set("fdMiningName", UserSession.getInSessionProperty("miningName"));//矿名称的默认值
		
		return outInfo;
	}
	/**
	 * 新增
	 * <p>Function: insert</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCI60</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-8-31 15:33:46 王伟
	 * </p>
	 *
	 */ 
	@Override
	public EiInfo insert(EiInfo inInfo) {	
		EiInfo out=new EiInfo();
		ZMCIWorkGroupInfo _dom = new ZMCIWorkGroupInfo();	
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;		
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			String fdStatus = "草拟";			
//			boolean validate = validate(inInfo, _dom,"ZMCGMineReportDispatch.validate",params);
//			if(validate) {
//				inInfo = this.query(inInfo);
//				return inInfo;
//			}else {
				_dom.setFdStatus(fdStatus);
				_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CI_WORKGROUP"));//主键
				_dom.setFdMiningId(UserSession.getInSessionProperty("miningCode").toString());	//矿井id
				_dom.setFdMiningName(UserSession.getInSessionProperty("miningName").toString());// 矿井名称
				this.dao.insert("ZMCIWorkGroupInfo.insert",_dom);
//			}
		}
		out = this.query(inInfo);
		//out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out; 	

} 

	/**
	 * 修改
	 * <p>Function: update</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-8-31 15:33:46 王伟
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCIWorkGroupInfo _dom = new ZMCIWorkGroupInfo();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String>	params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			
//			boolean validate = validate(inInfo, _dom,"ZMCGMineReportDispatch.validate",params);
//			if(validate) {
//				out = this.query(inInfo);
//				return out;
//			}
//			else {
				this.dao.update("ZMCIWorkGroupInfo.update",_dom);
//			}
		}
		out = this.query(inInfo);
//		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;	
			
	}		
			
	
	/**
	 * 删除
	 * <p>Function: delete</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCP70</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016-8-31 15:33:46 王伟
	 * </p>
	 *
	 */
	@Override
	public EiInfo delete(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCIWorkGroupInfo _dom = new ZMCIWorkGroupInfo();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCIWorkGroupInfo.delete", _dom);			
		}
		out = this.query(inInfo);
		//out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;   
	}

}
